/*
  1.创建两个线程，其中线程2执行过程中自己分离自己
  2.main线程中join两个线程
  3.join线程1成功，返回码为0，pthread_join返回0
  4.join线程2失败，返回码随机，pthread_join返回22，表示线程不可链接
*/

#include "spu.h"

void *thread_fun1(void *arg)
{
    for(int i = 0; i< 10; i++)
        printf("thread01 : %d\n", i);
	return (void*)0;
}

void *thread_fun2(void *arg)
{
    for(int i = 100; i< 110; i++)
    {
        printf("thread02 : %d\n", i);
        pthread_detach(pthread_self());   //分离线程，分离自己，参数：线程id
    }
	return (void*)0;
}

int main(int argc, char *argv[])
{	
	int err1, err2;
	pthread_t thread_id1,thread_id2; 
    
    void * __thread_return1, * __thread_return2;

	err1 = pthread_create(&thread_id1, NULL, thread_fun1, NULL);
	err2 = pthread_create(&thread_id2, NULL, thread_fun2, NULL);

	if(err1 || err2)
		printf("create failed!\n");
	else
		printf("create success!\n");


    printf("join thread01 return %d\n",pthread_join(thread_id1, &__thread_return1));
    printf("join thread02 return %d\n",pthread_join(thread_id2, &__thread_return2));

    printf("线程1返回码 %d\n",__thread_return1);
    printf("线程2返回码 %d\n",__thread_return2);

    return 0;
}

